package com.hoo.veiw.xml;

import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.BeansException;
import org.springframework.oxm.Marshaller;
import org.springframework.validation.BindingResult;
import org.springframework.web.servlet.view.xml.MarshallingView;

/**
 * <b>function:</b>继承MarshallingView，重写locateToBeMarshalled方法； 解决对象添加到ModelAndView中，转换后的xml是BindingResult信息的bug
 * 
 * @author hoojo
 * @createDate 2010-11-29 下午05:58:45
 * @file OverrideMarshallingView.java
 * @package com.hoo.veiw.xml
 * @project Spring3
 * @blog http://blog.csdn.net/IBM_hoojo
 * @email hoojo_@126.com
 * @version 1.0
 */
public class OverrideMarshallingView extends MarshallingView {

    private Marshaller marshaller;

    private String modelKey;

    public OverrideMarshallingView() {
        super();
    }

    public OverrideMarshallingView(Marshaller marshaller) {
        super(marshaller);
        this.marshaller = marshaller;
    }

    public void setMarshaller(Marshaller marshaller) {
        super.setMarshaller(marshaller);
        this.marshaller = marshaller;
    }

    public void setModelKey(String modelKey) {
        super.setModelKey(modelKey);
        this.modelKey = modelKey;
    }

    @Override
    protected void initApplicationContext() throws BeansException {
        super.initApplicationContext();
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Override
    protected void renderMergedOutputModel(Map model, HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        super.renderMergedOutputModel(model, request, response);
    }

    @Override
    protected Object locateToBeMarshalled(Map<String, Object> model) throws IllegalStateException {
        if (modelKey != null) {
            Object o = model.get(modelKey);
            if (!this.marshaller.supports(o.getClass())) {
                try {
                    throw new ServletException("Model object [" + o + "] retrieved via key [" + modelKey
                            + "] is not supported by the Marshaller");
                } catch (ServletException e) {
                    e.printStackTrace();
                }
            }
            return o;
        }
        for (Object o : model.values()) {

            // 解决对象添加到ModelAndView中，转换后的xml是BindingResult信息的bug
            if (o instanceof BindingResult) {
                continue;
            }

            if (this.marshaller.supports(o.getClass())) {
                return o;
            }
        }
        return null;

        // return super.locateToBeMarshalled(model);
    }

    // @SuppressWarnings("unchecked")
    // protected Object locateToBeMarshalled(Map model) throws ServletException {
    // if (modelKey != null) {
    // Object o = model.get(modelKey);
    // if (!this.marshaller.supports(o.getClass())) {
    // throw new ServletException("Model object [" + o + "] retrieved via key [" + modelKey
    // + "] is not supported by the Marshaller");
    // }
    // return o;
    // }
    // for (Object o : model.values()) {
    //
    // // 解决对象添加到ModelAndView中，转换后的xml是BindingResult信息的bug
    // if (o instanceof BindingResult) {
    // continue;
    // }
    //
    // if (this.marshaller.supports(o.getClass())) {
    // return o;
    // }
    // }
    // return null;
    // }
}
